<template>
  <div class="role-container">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="handleClick(null, 0, '新增角色')"
          >新增</el-button
        >
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          width="30%"
        >
          <el-form
            label-position="left"
            label-width="100px"
            :model="roleForm"
            :rules="rules"
            ref="roleForm"
          >
            <el-form-item label="名称" prop="name">
              <el-input
                :disabled="formType === 1"
                v-model="roleForm.name"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item label="标识" prop="sign">
              <el-input
                :disabled="formType === 1"
                v-model="roleForm.sign"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input
                :disabled="formType === 1"
                v-model="roleForm.description"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item prop="status" label="启用">
              <el-switch
                :disabled="formType === 1"
                v-model="roleForm.status"
                active-value="0"
                inactive-value="1"
                active-color="#13ce66"
                inactive-color="#ff4949"
              ></el-switch>
            </el-form-item>
            <el-form-item label="菜单" prop="menuIds">
              <el-tree
                @check-change="menuCheckChange"
                :data="menuDatas"
                show-checkbox
                node-key="id"
                :check-strictly="true"
                :default-checked-keys="roleForm.menuIds"
                ref="menuTree"
                :props="{ children: 'children', label: 'title' }"
              ></el-tree>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              v-if="formType !== 1"
              :loading="loading"
              type="primary"
              @click="confirmHandel('roleForm')"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table :data="pagination.roleDatas" row-key="id" border>
          <el-table-column type="index" width="30px"></el-table-column>
          <el-table-column prop="sign" label="标识"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="description" label="描述"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status === '0' ? 'success' : 'danger'"
                disable-transitions
                >{{ scope.row.status === "0" ? "启用" : "禁用" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150px">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row, 1, '角色详情')"
                type="text"
                size="small"
                >查看</el-button
              >
              <el-button
                @click="handleClick(scope.row, 2, '编辑角色')"
                type="text"
                size="small"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.sign !== 'SUPER_ADMIN'"
                @click="delRole(scope.row)"
                type="text"
                size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizeChoose"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getPaginationList,
  add,
  delRole,
  getDetails,
  updateRole,
} from "@/api/admin/role";
import { getMenuDetailVoList } from "@/api/admin/permission";

export default {
  name: "Menu",
  created() {
    this.getPaginationRoles();
  },
  data() {
    return {
      menuDatas: [],
      formType: 0,
      loading: false,
      pageRoles: [],
      dialogVisible: false,
      dialogTitle: "",
      roleForm: {
        name: null,
        sign: null,
        description: null,
        menuIds: [],
        status: "0",
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizeChoose: [5, 10, 15, 20],
        total: 0,
        roleDatas: [],
      },
      rules: {
        sign: [{ required: true, message: "请输入标识", trigger: "blur" }],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        description: [
          { required: true, message: "请输入描述", trigger: "blur" },
        ],
      },
    };
  },

  watch: {
    dialogVisible: function () {
      if (this.dialogVisible === false) {
        this.$refs["roleForm"].resetFields();
      } else {
        getMenuDetailVoList().then((response) => {
          this.menuDatas = response.data;
        });
      }
    },
  },
  methods: {
    delRole(role) {
      this.$confirm(
        '是否确认删除角色名为"' + role.name + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRole(role.id);
        })
        .then(() => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getPaginationRoles();
        })
        .catch(function () {});
    },
    addRole() {
      this.loading = true;
      add(this.roleForm)
        .then(() => {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationRoles();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    updateRole() {
      this.loading = true;
      updateRole(this.roleForm)
        .then(() => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationRoles();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    menuCheckChange(data, isCheck, haveChildren) {
      if (!isCheck && !haveChildren) {
        this.roleForm.menuIds.splice(this.roleForm.menuIds.indexOf(data.id), 1);
      } else if (isCheck) {
        this.roleForm.menuIds.push(data.id)
      }
    },
    confirmHandel(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formType === 0) {
            this.addRole();
          } else if (this.formType === 2) {
            this.updateRole();
          }
          this.loading = true;
        }
      });
    },
    getPaginationRoles() {
      const params = {
        pageSize: this.pagination.pageSize,
        pageIndex: this.pagination.currentPage,
      };
      getPaginationList(params).then((res) => {
        this.pagination.roleDatas = res.data.records;
        this.pagination.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.getPaginationRoles();
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val;
      this.getPaginationRoles();
    },
    handleClick(data, type, title) {
      if (type !== 0) {
        getDetails(data.id).then((response) => {
          this.roleForm = response.data;
        });
      }
      this.dialogTitle = title;
      this.formType = type;
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>